package com.b2mining.integration.xml;

import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.text.SimpleDateFormat;

import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.mapping.MappingException;
import org.jdom.DocType;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

import com.b2mining.integration.beans.Detraccion;
import com.b2mining.integration.beans.XMLBean;
import com.b2mining.integration.helper.Constantes;
import com.b2mining.integration.helper.RequiredElementHelper;

/**
 * Clase 
 * @since 31/07/2007
 * @author adminebiz <BR>
 * Analista Programador: Richard Linares
 */
public class DetraccionMessageXMLMapper extends XMLMapper {
	 static DetraccionMessageXMLMapper controller;    
	    Mapping map;

	    public static DetraccionMessageXMLMapper getInstance() throws IOException, MappingException{
	        if(controller==null){
	            controller = new DetraccionMessageXMLMapper();
	        }
	        return controller;
	    }
	    
	public void generateXML(XMLBean bean, Writer stream) throws XMLMapperException {
		
		XMLOutputter outputter = new XMLOutputter();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constantes.DATEFORMAT);
        outputter.setNewlines(true);
        outputter.setIndent(Constantes.TAB);
        outputter.setEncoding(Constantes.ENCODING_XML);
        Document doc = new Document();
        DocType dt = new DocType("DETSTATUS", "det_status_mq.dtd");
        Element element = null;
        doc.setDocType(dt);
        Detraccion detraccion = (Detraccion)bean;
        Element root = new Element("DETSTATUS");
        doc.setRootElement(root);
        
		//String documentName = root.getName();
		//String value=null;
		// aav.

		element = new Element("Fecha");
		element.setText(simpleDateFormat.format(detraccion.getFechaHoraPago()));
		root.addContent(element);
		
		element = new Element("NumeroDetraccion");
		element.setText(detraccion.getNumeroConstancia());
		root.addContent(element);
		
		element = new Element("RucComprador");
		element.setText(detraccion.getRucComprador());
		root.addContent(element);
		
		element = new Element("RucProveedor");
		element.setText(detraccion.getRucProveedor());
		root.addContent(element);
		
		element = new Element("Estado");
		element.setText(detraccion.getStatus());
		root.addContent(element);

        try
        {
            outputter.output(doc, stream);
        }
        catch(Exception e)
        {
            throw new XMLMapperException("Error al tratar de escribir el XML DetStatus hacia la ruta de salida.");
        }
		
	}

	public XMLBean parseXML(Reader stream, Reader streamAux) throws XMLMapperException {
		Detraccion detraccion = null;
		SimpleDateFormat m = new SimpleDateFormat(Constantes.DATEFORMAT);
		Document doc = null;			
		XMLMapperException ex = new XMLMapperException();

		try {
			doc = validaDocumento(stream, streamAux , ex);
			if (doc==null) {
				throw ex;
			}
		     Element docroot = doc.getRootElement();

			RequiredElementHelper reHelper= new RequiredElementHelper();
			String documentName = docroot.getName();
			String value=null;


			detraccion = new Detraccion();
			
			value =  reHelper.checkValue( docroot.getChild("Fecha")  );
		 	detraccion.setFechaHoraPago(m.parse(value));

			value =  reHelper.checkValue( docroot.getChild("NumeroDetraccion")  );
            detraccion.setNumeroConstancia(value);
            
            value =  reHelper.checkValue( docroot.getChild("RucComprador")  );
            detraccion.setRucComprador(value);
            
            value =  reHelper.checkValue( docroot.getChild("RucProveedor")  );
            detraccion.setRucProveedor(value);

            value = reHelper.checkValue(docroot.getChild("Estado"));
            detraccion.setStatus(value);
			
		}
		catch(Exception e){
			throw new XMLMapperException("Error en parseo de XML" + e.toString());
		}
			//aav.
		if (ex.getChildrenCount()>0) {
			throw ex;
		}
		//.		
		return detraccion;
	}

	protected void validaContenido(Document doc, XMLMapperException ex) {
		try {
			Element docroot = doc.getRootElement();
			RequiredElementHelper reHelper= new RequiredElementHelper();
			String documentName = docroot.getName();
			String value=null;
			value =  reHelper.checkValue( docroot.getChild("Fecha")  );
			if (!reHelper.isCorrect(documentName,"Fecha", value )) {
				MissingElementException mee = new MissingElementException();
				mee.setElement("Fecha");
				mee.setParent(docroot);
				ex.addChild(mee);
			}

			value =  reHelper.checkValue( docroot.getChild("NumeroDetraccion")  );
			if (!reHelper.isCorrect(documentName,"NumeroDetraccion", value ))  {
                MissingElementException mee = new MissingElementException();
                mee.setElement("NumeroDetraccion");
                mee.setParent(docroot);
                ex.addChild(mee);
            }
			
			value =  reHelper.checkValue( docroot.getChild("RucComprador")  );
			if (!reHelper.isCorrect(documentName,"RucComprador", value ))  {
                MissingElementException mee = new MissingElementException();
                mee.setElement("RucComprador");
                mee.setParent(docroot);
                ex.addChild(mee);
            }
			
			value =  reHelper.checkValue( docroot.getChild("RucProveedor")  );
			if (!reHelper.isCorrect(documentName,"RucProveedor", value ))  {
                MissingElementException mee = new MissingElementException();
                mee.setElement("RucProveedor");
                mee.setParent(docroot);
                ex.addChild(mee);
            }
			
			value = reHelper.checkValue(docroot.getChild("Estado"));
			if(!reHelper.isCorrect(documentName,"Estado", value)){
				MissingElementException mee = new MissingElementException();
				mee.setElement("Estado");
				mee.setParent(docroot);
				ex.addChild(mee);
			}
            
		}
		catch(Exception e){
			ex.addChild(new  XMLMapperException(e.toString()));
		}
	}

	public void generateXML(XMLBean beanList, Writer stream, int contador)
			throws XMLMapperException {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void validaContenido2(Document doc, XMLMapperException ex) {
		// TODO Auto-generated method stub
		
	}

	
}
